from flask import current_app
from flask import render_template

from business.models import News, Product
from business.modules.index import index_blu


@index_blu.route('/')
def index():
    """
    显示首页
    :return:
    """

    # 1 新闻咨询的展示
    news_list = []
    try:
        news_list = News.query.order_by(News.create_time.desc()).limit(6)
    except Exception as e:
        current_app.logger.error(e)

    # 定义一个空的字典列表，里面装的就是字典
    news_dict_li = []
    # 遍历对象列表，将对象的字典添加到字典列表中
    for news in news_list:
        news_dict_li.append(news.to_basic_dict())

    # 2  产品展示
    product_list = []
    try:
        product_list = Product.query.order_by(Product.create_time.desc()).limit(4)
    except Exception as e:
        current_app.logger.error(e)

    # 定义一个空的字典列表，里面装的就是字典
    product_dict_li = []
    # 遍历对象列表，将对象的字典添加到字典列表中
    for product in product_list:
        product_dict_li.append(product.to_basic_dict())

    data = {
        "news_dict_li": news_dict_li,
        "product_dict_li": news_dict_li,
    }

    return render_template('front/index.html', data=data)

